package com.kcsj.service;
//姜瑞临
import com.kcsj.dao.InventoryDao;
import com.kcsj.domain.Inventory;
import com.kcsj.domain.ProductModel;
import com.kcsj.domain.WarehouseInputRecord;
import com.kcsj.domain.WarehouseOutRecord;
import com.kcsj.util.Condition;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class InventoryService {
    private static InventoryDao inventoryDao = InventoryDao.getInstance();
    private static InventoryService inventoryService =new InventoryService();
    private InventoryService(){}
    public static InventoryService getInstance(){
        return inventoryService;
    }

    public void add(Inventory inventory) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean add = inventoryDao.add(inventory, conn);
        JdbcHelper.close(conn);
    }

    public Collection<Inventory> find(String warehouseNo) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Inventory> inventories = inventoryDao.find(warehouseNo,conn);
        JdbcHelper.close(conn);
        return inventories;
    }

    public boolean update(int changeNumber, ProductModel productModel, String type) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        if (type.equals("盘盈")){
            WarehouseInputRecord warehouseInputRecord = new WarehouseInputRecord();
            warehouseInputRecord.setProductModel(productModel);
            warehouseInputRecord.setTagInformation("盘点");
            warehouseInputRecord.setRemarks(type);
            int affected=0;
            for (int i=1;i<=changeNumber;i++){
                affected += WarehouseInputRecordService.getInstance().add(warehouseInputRecord,changeNumber);
            }
            return affected==changeNumber;
        }else if (type.equals("盘亏")){
            WarehouseOutRecord warehouseOutRecord = new WarehouseOutRecord();
            warehouseOutRecord.setProductModel(productModel);
            warehouseOutRecord.setTagInformation("盘点");
            warehouseOutRecord.setRemarks(type);
            int affected=0;
            for (int i=1;i<=changeNumber;i++){
                affected = WarehouseOutRecordService.getInstance().add(warehouseOutRecord,changeNumber);
            }
            return affected==changeNumber;
        }
        JdbcHelper.close(conn);
        return false;
    }
    public Collection<Inventory> findAll(List<Condition> conditionList) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Inventory> inventories = inventoryDao.findAll(conn,conditionList);
        JdbcHelper.close(conn);
        return inventories;
    }
}
